# =============================================================================
# XLLM Ops CMake Configuration
# =============================================================================

# Common include directories for all targets
set(ASCEND_COMMON_INCLUDES
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_SOURCE_DIR}/src/common/inc
    ${ASCEND_CANN_PACKAGE_PATH}/include
    ${ASCEND_CANN_PACKAGE_PATH}/include/external
    ${ASCEND_CANN_PACKAGE_PATH}/include/experiment
    ${ASCEND_CANN_PACKAGE_PATH}/include/experiment/platform
    ${ASCEND_CANN_PACKAGE_PATH}/include/experiment/metadef
    ${ASCEND_CANN_PACKAGE_PATH}/include/experiment/runtime
    ${ASCEND_CANN_PACKAGE_PATH}/include/experiment/msprof
)

# Common compile options for ops
set(COMMON_OPS_OPTIONS
    --cce-auto-sync=on
    -Wno-deprecated-declarations
    -Werror
)

# =============================================================================
# Debug Build Configuration
# =============================================================================
if ("${CMAKE_BUILD_TYPE}x" STREQUAL "Debugx")
    add_ops_compile_options(
        OP_NAME ALL 
        OPTIONS -g -O0
    )
endif()

# =============================================================================
# Compile Options Configuration
# =============================================================================
add_ops_compile_options(
    OP_NAME replace_token
    OPTIONS ${COMMON_OPS_OPTIONS}
)

add_ops_compile_options(
    OP_NAME ApplyTopKTopPWithSorted
    OPTIONS ${COMMON_OPS_OPTIONS}
)

add_ops_compile_options(
    OP_NAME PpMatmulOpt
    OPTIONS ${COMMON_OPS_OPTIONS}
)

add_ops_compile_options(
    OP_NAME PpMatMul
    OPTIONS ${COMMON_OPS_OPTIONS}
)

# =============================================================================
# Target Sources Configuration - optiling (All together)
# =============================================================================
target_sources(optiling PRIVATE
    # replace_token
    replace_token_cpu.cpp
    
    # index_group_matmul
    index_group_matmul_cpu.cpp
    
    # ApplyTopKTopPWithSorted
    apply_top_k_top_p_with_sorted_tiling.cc
    
    # PpMatmulOpt
    pp_matmul_opt_cpu.cpp
    
    # PpMatMul
    pp_mat_mul_tiling.cpp
    pp_mat_mul_default.cpp
)

target_include_directories(optiling PRIVATE
    ${ASCEND_COMMON_INCLUDES}
)

# =============================================================================
# Target Sources Configuration - op_host_aclnn
# =============================================================================
target_sources(op_host_aclnn PRIVATE
    replace_token_cpu.cpp
    index_group_matmul_cpu.cpp
    pp_matmul_opt_cpu.cpp
    pp_mat_mul_cpu.cpp
)

# =============================================================================
# Target Sources Configuration - opsproto
# =============================================================================
target_sources(opsproto PRIVATE
    replace_token_cpu.cpp
    index_group_matmul_cpu.cpp
    pp_matmul_opt_cpu.cpp
    pp_mat_mul_cpu.cpp
)

target_include_directories(opsproto PRIVATE
    ${ASCEND_COMMON_INCLUDES}
)

# =============================================================================
# Target Sources Configuration - op_host_aclnnInner
# =============================================================================
target_sources(op_host_aclnnInner PRIVATE
    apply_top_k_top_p_with_sorted_cpu.cpp
)

target_include_directories(op_host_aclnnInner PRIVATE
    ${ASCEND_COMMON_INCLUDES}
)

# =============================================================================
# Target Sources Configuration - opapi
# =============================================================================
target_sources(opapi PRIVATE
    apply_top_k_top_p_with_sorted_l0.cpp
    aclnn_apply_top_k_top_p.cpp
)

# =============================================================================
# Install Files Configuration
# =============================================================================
install(FILES 
    replace_token.cpp
    index_group_matmul.cpp
    index_group_matmul.h
    index_group_matmul_mlt512.h
    apply_top_k_top_p_with_sorted.cpp
    apply_top_k_top_p_with_sorted.h
    pp_matmul_opt.cpp
    pp_mat_mul.cpp
    DESTINATION ${ASCEND_IMPL_OUT_DIR}/dynamic
)

install(FILES aclnn_apply_top_k_top_p.h
    DESTINATION ${ACLNN_INC_INSTALL_DIR} OPTIONAL
)